using System;
using System.IO;
using System.Windows.Forms;

namespace FileMergerApp
{ public partial class MainForm : Form
    {
        private string filePath1 = "";
        private string filePath2 = "";
        public MainForm()
        {
            InitializeComponent();
            this.Text = "文本文件合并工具";
            this.Size = new System.Drawing.Size(500, 300);
        }
        private void InitializeComponent()
        {
            // 文件1选择
            Label label1 = new Label
            {
                Text = "文件1:",
                Location = new System.Drawing.Point(20, 20),
                Size = new System.Drawing.Size(50, 20)
            };
            
            TextBox textBox1 = new TextBox
            {
                Location = new System.Drawing.Point(80, 20),
                Size = new System.Drawing.Size(300, 20),
                ReadOnly = true
            };
            
            Button button1 = new Button
            {
                Text = "浏览...",
                Location = new System.Drawing.Point(390, 20),
                Size = new System.Drawing.Size(80, 23)
            };
            button1.Click += (s, e) => SelectFile(ref filePath1, textBox1);

            // 文件2选择
            Label label2 = new Label
            {
                Text = "文件2:",
                Location = new System.Drawing.Point(20, 60),
                Size = new System.Drawing.Size(50, 20)
            };
            
            TextBox textBox2 = new TextBox
            {
                Location = new System.Drawing.Point(80, 60),
                Size = new System.Drawing.Size(300, 20),
                ReadOnly = true
            };
            
            Button button2 = new Button
            {
                Text = "浏览...",
                Location = new System.Drawing.Point(390, 60),
                Size = new System.Drawing.Size(80, 23)
            };
            button2.Click += (s, e) => SelectFile(ref filePath2, textBox2);

            // 合并
            Button mergeButton = new Button
            {
                Text = "合并文件",
                Location = new System.Drawing.Point(200, 120),
                Size = new System.Drawing.Size(100, 30)
            };
            mergeButton.Click += MergeFiles;

            // 状态标签
            Label statusLabel = new Label
            {
                Location = new System.Drawing.Point(20, 170),
                Size = new System.Drawing.Size(450, 60),
                Text = "请选择两个文本文件进行合并"
            };
            // 添加控件到窗体
            this.Controls.Add(label1);
            this.Controls.Add(textBox1);
            this.Controls.Add(button1);
            this.Controls.Add(label2);
            this.Controls.Add(textBox2);
            this.Controls.Add(button2);
            this.Controls.Add(mergeButton);
            this.Controls.Add(statusLabel);
        }

        private void SelectFile(ref string filePath, TextBox textBox)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Filter = "文本文件|*.txt|所有文件|*.*",
                Title = "选择文本文件"
            };

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = openFileDialog.FileName;
                textBox.Text = filePath;
            }
        }
        private void MergeFiles(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(filePath1) || string.IsNullOrEmpty(filePath2))
            {
                MessageBox.Show("请先选择两个文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            try
            {
                // 确保Data目录存在
                string dataDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
                if (!Directory.Exists(dataDir))
                {
                    Directory.CreateDirectory(dataDir);
                }
                // 生成合并后的文件名
                string outputFileName = $"merged.txt";
                string outputPath = Path.Combine(dataDir, outputFileName);
                // 读取并合并文件内容
                string content1 = File.ReadAllText(filePath1);
                string content2 = File.ReadAllText(filePath2);
                string mergedContent = content1 + Environment.NewLine + content2;
                // 写入新文件
                File.WriteAllText(outputPath, mergedContent);
                MessageBox.Show($"文件合并成功！\n保存路径: {outputPath}", "成功", 
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"合并文件时出错: {ex.Message}", "错误", 
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}